
# GenticCore.rb
# Genetic Programming functions.

class GeneticCore

  attr_reader :code, :TERMINAL_SET, :FUNCTION_SET

  def initialize
    @code = ""

    @TERMINAL_SET = %w(turn_left turn_right accelerate shoot stop)
    @FUNCTION_SET = %w(a_then_b enemy_on_sight?)
  end

  def random_choice(list)
    max_random = list.size
    random     = rand(max_random) 
    list[random]		
  end

  def generate_code(curr_depth=0, min_depth=2, max_depth=4)
    
    if curr_depth == max_depth
      @code = "(" + random_choice(@TERMINAL_SET) + "," + random_choice(@TERMINAL_SET) + ")" 
      
    elsif curr_depth < min_depth
      next_depth = curr_depth + 1
      @code = random_choice(@FUNCTION_SET) + "("  + generate_code(next_depth, min_depth, max_depth) + "," + 
        generate_code(next_depth, min_depth, max_depth) + ")"
      
    else
      next_depth = curr_depth + 1
      total_set  = @FUNCTION_SET + @TERMINAL_SET
      @code = random_choice(@FUNCTION_SET) + "("  + generate_code(next_depth, min_depth, max_depth) + "," + 
        generate_code(next_depth, min_depth, max_depth) + ")"

      @code = "(" + random_choice(@FUNCTION_SET + @TERMINAL_SET) + generate_code(curr_depth + 1, min_depth, max_depth) + ")"	
    end

  end

end
